//
//  RBRegisterStateVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/5.
//

#import "RBRegisterStateVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import <Masonry.h>
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBThemeConfig.h"
#import "RBRegisterDetailResponse.h"
#import "UIViewController+RBAdd.h"
#import "UINavigationController+RBAdd.h"
#import "RBRegisterCompleteInfoVC.h"
#import "RBAccountManager.h"
#import "RBAppDelegateHelper.h"

@interface RBRegisterStateVC ()

@property(nonatomic, strong)IBOutlet UIView *topView;
@property(nonatomic, strong)IBOutlet UIView *dot1;
@property(nonatomic, strong)IBOutlet UIView *dot2;
@property(nonatomic, strong)IBOutlet UIView *dot3;
@property(nonatomic, strong)IBOutlet UILabel *textL1;
@property(nonatomic, strong)IBOutlet UILabel *textL2;
@property(nonatomic, strong)IBOutlet UILabel *textL3;
@property(nonatomic, strong)IBOutlet UIView *line1To2;
@property(nonatomic, strong)IBOutlet UIView *line2To3;

@property(nonatomic, strong) RBRegisterDetailResponse *detailResponse;

@end

@implementation RBRegisterStateVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"审核状态";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    [self.topView zoomSubViewsWithSelf];
    self.topView.height = Zoom(self.topView.height);
    if(self.loginResponse.data.step == 4)
    {
        [self getData];
    }
    else
    {
        [self fillData];
    }
}

- (void)fillData
{
    //0审核中 1审核通过 2审核未通过
    NSInteger status = self.loginResponse.data.step - 2;
    
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Zoom(47));
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(self.topView.height);
    }];
    
    UIColor *dotBlue = [UIColor colorWithHexString:@"#518CFF"];
    UIColor *dotGray = [UIColor colorWithHexString:@"#DFDFDF"];
    UIColor *dotRed = [UIColor colorWithHexString:@"#E60012"];
    
    if (status == 0)
    {
        self.dot1.backgroundColor = dotBlue;
        [self setupImage:[UIImage imageNamed:@"registerStatePass"] withDot:self.dot1];
        self.textL1.text = @"注册成功";
        
        self.line1To2.backgroundColor = dotBlue;
        
        self.dot2.backgroundColor = dotBlue;
        [self setupImage:[UIImage imageNamed:@"registerStatePass"] withDot:self.dot2];
        self.textL2.text = @"资料审核中";
        
        self.line2To3.backgroundColor = dotGray;
        
        self.dot3.backgroundColor = dotGray;
        [self setupImage:[UIImage imageNamed:@"registerStatePass"] withDot:self.dot3];
        self.textL3.text = @"已通过";
        
        UIImageView *statusImgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"registerStateLoading"]];
        [self.view addSubview:statusImgView];
        [statusImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.topView.mas_bottom).offset(Zoom(65));
            make.width.height.mas_equalTo(Zoom(124));
        }];
        
        UILabel *tipL = [self tipLabelWithTip:@"您的信息已提交，请耐心等待"];
        [self.view addSubview:tipL];
        [tipL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(statusImgView.mas_bottom).offset(Zoom(23));
        }];
        
        if(![self inTabbarController])
        {
            UIButton *touristBtn = [self actionBtnWithTitle:@"游客登录"];
            [touristBtn addTarget:self action:@selector(enterAsTourist) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:touristBtn];
            [touristBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(touristBtn.size);
                make.centerX.mas_equalTo(0);
                make.bottom.mas_equalTo(-MAX(Zoom(27), kRBStatusBarHeight));
            }];
        }
    }
    else if (status == 1)
    {
        self.dot1.backgroundColor = dotBlue;
        [self setupImage:[UIImage imageNamed:@"registerStatePass"] withDot:self.dot1];
        self.textL1.text = @"注册成功";
        
        self.line1To2.backgroundColor = dotBlue;
        
        self.dot2.backgroundColor = dotBlue;
        [self setupImage:[UIImage imageNamed:@"registerStatePass"] withDot:self.dot2];
        self.textL2.text = @"资料审核";
        
        self.line2To3.backgroundColor = dotBlue;
        
        self.dot3.backgroundColor = dotBlue;
        [self setupImage:[UIImage imageNamed:@"registerStatePass"] withDot:self.dot3];
        self.textL3.text = @"已通过";
        
        UILabel *tipL = [self tipLabelWithTip:@"恭喜，您的店铺审核已通过"];
        [self.view addSubview:tipL];
        [tipL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.topView.mas_bottom).offset(Zoom(70));
        }];
        
        if(![self inTabbarController])
        {
            UIButton *touristBtn = [self actionBtnWithTitle:@"进入主页"];
            [touristBtn addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:touristBtn];
            [touristBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(touristBtn.size);
                make.centerX.mas_equalTo(0);
                make.bottom.mas_equalTo(-MAX(Zoom(27), kRBStatusBarHeight));
            }];
        }
    }
    else if (status == 2)
    {
        self.dot1.backgroundColor = dotBlue;
        [self setupImage:[UIImage imageNamed:@"registerStatePass"] withDot:self.dot1];
        self.textL1.text = @"注册成功";
        
        self.line1To2.backgroundColor = dotBlue;
        
        self.dot2.backgroundColor = dotBlue;
        [self setupImage:[UIImage imageNamed:@"registerStatePass"] withDot:self.dot2];
        self.textL2.text = @"资料审核中";
        
        self.line2To3.backgroundColor = dotGray;
        
        self.dot3.backgroundColor = dotRed;
        [self setupImage:[UIImage imageNamed:@"registerStateDeny"] withDot:self.dot3];
        self.textL3.text = @"未通过";
        
        UILabel *tipL = [self tipLabelWithTip:@"很遗憾，您的审核未通过"];
        [self.view addSubview:tipL];
        [tipL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.topView.mas_bottom).offset(Zoom(70));
        }];
        
        UIView *reasonContainer = UIView.new;
        reasonContainer.backgroundColor = UIColor.whiteColor;
        [reasonContainer rbSetCornerRadius:Zoom(8)];
        [self.view addSubview:reasonContainer];
        [reasonContainer mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(Zoom(350));
//            make.height.mas_equalTo(Zoom(156));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(tipL.mas_bottom).offset(Zoom(27));
        }];
        
        UILabel *reasonTitleL = UILabel.new;
        reasonTitleL.text = @"拒绝理由：";
        reasonTitleL.font = [UIFont systemFontOfSize:Zoom(12)];
        reasonTitleL.textColor = [RBThemeConfig textBlackColor];
        [reasonContainer addSubview:reasonTitleL];
        [reasonTitleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Zoom(13));
            make.top.mas_equalTo(Zoom(17));
        }];
        
        UILabel *reasonL = UILabel.new;
        reasonL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:reasonTitleL.font color:reasonTitleL.textColor string:self.detailResponse.data.fail_reason.length? self.detailResponse.data.fail_reason: @"无" lineSpacing:Zoom(7)];
        }];
        reasonL.numberOfLines = 0;
        [reasonContainer addSubview:reasonL];
        [reasonL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Zoom(78));
            make.top.mas_equalTo(Zoom(17));
            make.right.mas_equalTo(-Zoom(13));
            make.bottom.mas_equalTo(-Zoom(17));
        }];
        
        UIButton *reCommitBtn = [self actionBtnWithTitle:@"重新提交资料"];
        [reCommitBtn addTarget:self action:@selector(reCommit) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:reCommitBtn];
        [reCommitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(reCommitBtn.size);
            make.centerX.mas_equalTo(0);
            make.bottom.mas_equalTo(-MAX(Zoom(27), kRBStatusBarHeight));
        }];
        
        if(![self inTabbarController])
        {
            UIButton *touristBtn = [self actionBtnWithTitle:@"游客登录"];
            [touristBtn addTarget:self action:@selector(enterAsTourist) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:touristBtn];
            [touristBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(touristBtn.size);
                make.centerX.mas_equalTo(0);
                make.bottom.mas_equalTo(reCommitBtn.mas_top).offset(Zoom(-13));
            }];
        }
    }
}

- (void)setupImage: (UIImage *)image withDot: (UIView *)dot
{
    UIImageView *imgView = (UIImageView *)[dot viewWithTag:3];
    imgView.image = image;
}

- (BOOL)inTabbarController
{
    return self.tabBarController;
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"user_id"] = self.loginResponse.data.user_id;
    } path:@"han_user_detail_status" post:YES log:YES responseClass:RBRegisterDetailResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(RBRegisterDetailResponse *data) {
        weak_self.detailResponse = data;
        [weak_self fillData];
    } failBlock:^(RBRegisterDetailResponse *data) {
        
    } reloadBlock:^{
        [weak_self getData];
    }];
}

#pragma mark - Event

- (void)enterAsTourist
{
    [RBAccountManager setUserId:self.loginResponse.data.user_id];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)enter
{
    [RBAccountManager setUserId:self.loginResponse.data.user_id];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)reCommit
{
    RBRegisterCompleteInfoVC *vc = RBRegisterCompleteInfoVC.new;
    vc.loginResponse = self.loginResponse;
    vc.detailResponse = self.detailResponse;
    [self.navigationController pushToViewController:vc animated:YES removeVcCount:1];
}

#pragma mark - Setter

#pragma mark - Getter

- (UILabel *)tipLabelWithTip: (NSString *)tip
{
    UILabel *view = UILabel.new;
    view.text = tip;
    view.textColor = [RBThemeConfig textBlackColor];
    view.font = [UIFont boldSystemFontOfSize:Zoom(15)];
    return view;
}

- (UIButton *)actionBtnWithTitle: (NSString *)title
{
    UIButton *btn = UIButton.new;
    btn.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    btn.size = CGSizeMake(Zoom(334), Zoom(44));
    [btn rbSetCornerRadius:Zoom(22)];
    return btn;
}


@end
